<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;
use think\Request;

/**
 * 前台菜单
 * Class Channel
 * @package app\admin\controller
 */
class Channel extends Home
{
    /**
     * 前台栏目
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $pid = input('pid', 0);
        $list = Db('channel')->where('pid', $pid)->order('sort asc')->paginate(20);
        $page = $list->render();
        $this->assign('page', $page);
        $this->assign('_list', $list);

        // 记录当前列表页的cookie
        Cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('title', '前台管理');
        return $this->fetch();
    }

    /**
     * 编辑栏目
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function edit(Request $request)
    {
        $id = input('id');
        $category = Db('channel')->find($id);
        if ($request->isPost()) {
            $data = input('post.');
            $validate = Validate('Channel');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $id = Db('channel')->where('id',$id)->update($data);
            if ($id) {
                // S('DB_CONFIG_DATA',null);
                //记录行为
//                action_log('update_menu', 'Menu', $id, UID);
                $this->success('编辑成功', Cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        } else {
            $this->assign('info', $category);
            $this->assign('title', '编辑栏目');
            return $this->fetch();
        }
    }

    /**
     * 菜单排序
     * @author huajie <banhuajie@163.com>
     */
//    public function sort()
//    {
//        if (IS_GET) {
//            $ids = I('get.ids');
//            $pid = I('get.pid');
//
//            //获取排序的数据
//            $map = array('status' => array('gt', -1));
//            if (!empty($ids)) {
//                $map['id'] = array('in', $ids);
//            } else {
//                if ($pid !== '') {
//                    $map['pid'] = $pid;
//                }
//            }
//            $list = M('Menu')->where($map)->field('id,title')->order('sort asc,id asc')->select();
//
//            $this->assign('list', $list);
//            $this->meta_title = '菜单排序';
//            $this->display();
//        } elseif (IS_POST) {
//            $ids = I('post.ids');
//            $ids = explode(',', $ids);
//            foreach ($ids as $key => $value) {
//                $res = M('Menu')->where(array('id' => $value))->setField('sort', $key + 1);
//            }
//            if ($res !== false) {
//                $this->success('排序成功！');
//            } else {
//                $this->eorror('排序失败！');
//            }
//        } else {
//            $this->error('非法请求！');
//        }
//    }
}
